﻿var pop = {//dialog-con:802 dialog-msk:801 confirm-con:804 ,confirm-msk:803 ,alert-con:806 alert-msk:805
	defaultOption : {
		popTitle : "标题",
		popContent : "<div style='font-size:20px;text-align:center;'>弹窗内容未设定</div>",
		popOkText : "确认",
		popCancelText : "取消",
		popOkShow : true,
		popCancelShow : true,
		width : "400px",
		height : "",
		top:"10%",
		maxHeight : "",//($(window).height() - 202) + "px"
		onLoad:function(){},
		showPopFooter:true,
		footer:"",
		type:"center"//center:弹出框为中间弹出，right:弹出框右侧滑出
	},
	events : {
		".pop-box .input-clear click" : "inputClear",
		".pop-box .input-password click" : "showPassword",
		".pop-box .createds-input focus" : "showBtn",
		".pop-box #dsTest click" : "dsTest"
	},
	init: function($elem, finalOption,ok, cancel,close,level){
		var that = this;
		if (!$elem || $elem.length == 0) return;
		if(finalOption.type == "center"){
			$elem.appendTo($("body")).children().css({
				"width":finalOption.width,
				"height":finalOption.height,
				"top":finalOption.top,
				"left":"calc(50% - "+parseInt(finalOption.width)/2+"px)"
			});
		}else if(finalOption.type == "right"){
			$elem.appendTo($("body")).animate({
				right:0
			})
		}

		if($(".pop-box").length == 1){//首次弹窗绑定事件
			Util.event.call(this, this.events);
		}
		this.elem = $elem;
		this.screenSize();

		this.pos();


		$(".pop-container").resize(function(){
			//alert(1);
		});
		//var thisType = $elem.hasClass("box-dialog")?1:$elem.hasClass("box-confirm")?2:3;
		$elem.find(".pop-close").on("click", function(){
			close && close();
			$elem.remove();
			popHide($elem,finalOption.type);
		});
		$elem.find(".pop-cancel").on("click", function(){
			cancel && cancel();
			popHide($elem,finalOption.type);
		});
		$elem.find(".pop-ok").on("click", function(){
			if(Util.isEmpty(ok)){
				popHide($elem,finalOption.type);
			}else{
				ok($elem) != false ? popHide($elem,finalOption.type):"";
			}
		});
		function popHide($elem,type){
			if(type=="center"){
				$elem.remove()
			}else if(type == "right"){
				$elem.animate({right:"-405px"},300, function () {
					$elem.remove()
				})
			}
		}
		return this;
	},
	inputClear : function(elem){
		$(elem).parent().find("input,textarea").val("");
		$(elem).hide();
	},
	dsTest : function(){
		var dbIp = $("#dbIp").val(),
			dbUserName = $("#dbUserName").val(),
			dbPassword = $("#dbPassword").val(),
			url = interfaceURL.testsource();
		Util.ajaxRequest({
			url:url,
			type:"get",
			data:{
				url : dbIp,
				username : dbUserName,
				password : dbPassword
			},
			dataType:"json",
			success:function(json){
				if(json.code == 200){
					if(json.result){
						$("#dsTest").attr("flag","true");
						pop.alert("测试成功！");
					}else{
						$("#dsTest").attr("flag","error");
						pop.alert("测试失败，请修改参数！");
					}
				}
			}
		});
	},
	showPassword : function(elem){
		if($(elem).hasClass("fa-eye")){
			$(elem).removeClass("fa-eye");
			$(elem).addClass("fa-eye-slash");
			$(elem).parent().find("input").attr("type","password");
		}else{
			$(elem).removeClass("fa-eye-slash");
			$(elem).addClass("fa-eye");
			$(elem).parent().find("input").attr("type","text");
		}
		// $(elem).hide();
	},
	showBtn : function(elem){
		$(".createds-input").parent().find("i").hide();
		$(elem).parent().find("i").css("display","inline-block");
	},
	removeMask:function($elem,thisType){
		switch (thisType){
			case 1:$(".pop_dialog").remove();break;
			case 2:$(".pop_confirm").remove();break;
			case 3:$(".pop_alert").remove();break;
		}
		$elem.remove();
		$.isFunction(pop.defaultOption.unLoad)?pop.defaultOption.unLoad():"";
	},
	screenSize:function(){
		var doc = $(document);

		this.screen = {
			w:doc.width(),
			h:doc.height(),
			sh:$(window).height(),//可视高度
			sw:$(window).width()//可视宽度
		};
		return this;
	},
	pos:function(){
		var elem = this.elem,
			w = elem.width(),
			h = elem.outerHeight(true),//elem.height(),
			s = this.screen,
			scrollTop = 0;
		if (navigator.userAgent.indexOf('Firefox') >= 0){
			scrollTop = document.documentElement.scrollTop;
		}else{
			scrollTop = document.body.scrollTop;
		}
		this.posXY = {
			//x:(s.sh - h>>1) + (document.documentElement.scrollTop || document.body.scrollTop),
			x:(s.sh - h>>1) + scrollTop,
			y:(s.w - w)>>1
		};
		return this;
	},
	confirm:function(text, ok, cancel,close){
		var style = $(".resourceMange-body").length>0?"sky":"blue";
		var popContent = "<div style='font-size:16px;text-align:center;padding:20px 0;'>"+text+"</div>";
		var finalOption = $.extend({},pop.defaultOption,{popContent:popContent,popTitle:"确认"},{style:style});
		var $elem = $(Util.template($("#pop-confirm-template").html(),finalOption));
		this.init($elem,finalOption,ok,cancel,close,"confirm");
		this.resize(false,$elem)
	},
	alert:function(text, ok){
		var texts;
		var style = $(".resourceMange-body").length>0?"sky":"blue";
		var popContent = "<div style='font-size:16px;text-align:center;padding:20px 0;'>"+text+"</div>";
		var finalOption = $.extend({},pop.defaultOption,{popContent:popContent,popTitle:"提示"},{style:style});
		var $elem = $(Util.template($("#pop-alert-template").html(),finalOption));
		this.init($elem,finalOption,ok,"",null,"alert");
		this.resize(false,$elem)
	},
	dialog:function(option, ok, cancel,close){
		var _this = this;
		var style = $(".resourceMange-body").length>0?"sky":"blue";
		var finalOption = $.extend({},pop.defaultOption,option,{style:style});
		var $elem = "";
		if(finalOption.type=="right"){
			var $dr = $(".box-dialog.dialog-right");
			if($dr.length>0){
				$dr.animate({right:"-405px"},300, function () {
					$(this).remove();
					$elem = $(Util.template($("#pop-right-template").html(),finalOption));
					_this.init($elem,finalOption,ok, cancel,close,"dialog");
				});
			}else{
				$elem = $(Util.template($("#pop-right-template").html(),finalOption));
				_this.init($elem,finalOption,ok, cancel,close,"dialog");
			}
		}else{
			$elem = $(Util.template($("#pop-dialog-template").html(),finalOption));
			this.init($elem,finalOption,ok, cancel,close,"dialog");
			_this.resize(false,$elem);
		}
		finalOption.onLoad($elem);
	},
	mAlert: function (text) {
		var $easyHint = $(".easy-hint");
		if(!$easyHint.hasClass("doing")){
			$easyHint.addClass("doing").show().animate({
				height:"38px"
			},200, function () {
				var setTime = setTimeout(function () {
					closeHint()
				},2000);
				$easyHint.css("height","auto").off("click").click(function () {
					clearTimeout(setTime);
					closeHint()
				})
			}).find(".hint-body").html(text);
		}
		function closeHint(){
			$easyHint.animate({
				height:0
			},400, function () {
				$easyHint.hide().removeClass("doing");
			})
		}
	},
	resize: function (animate,ele) {
		var $dialog = Util.isEmpty(ele)?$(".box-dialog:not(.dialog-right)"):ele.find(".box-dialog:not(.dialog-right)");
		var isAnimate = Util.isEmpty(animate)||!animate?false:true;
		var A_height =  $(window).height();
		var B_height = $dialog.height();
		var A_top = $dialog.offset().top;
		var B_top = 0;
		if(A_height>B_height){
			B_top = (A_height - B_height)/2;
		}
		var cha = Math.abs(A_top - B_top);
		if(cha>12){
			if(isAnimate){
				$dialog.animate({
					top:B_top
				},300, function () {

				})
			}else{
				$dialog.css("top",B_top)
			}
		}
	}
};
(function () {
	$(".main").click(function () {
		var popRight = $(".box-dialog.dialog-right");
		if(popRight.length>0){
			popRight.animate({right:"-405px"},300, function () {
				popRight.remove();
			});
		}
	})
})();